<?php
namespace Devcopilot\Models;

use Devcopilot\Models\BaseModel;

class Task extends BaseModel
{
    protected $table = 'tasks';
    protected $fillable = [
        'name',
        'application_id',
        'version_id',
        'feature_id',
        'description',
        'task_type',
        'task_status',
        'task_priority',
        'start_time',
        'end_time'
    ];

    public function __construct($attributes = [])
    {
        parent::__construct($attributes);
    }

    public function getCount(string $type) {
        $query = $this->newQuery();
        if($type === 'pending') {
            // 开发中的版本
            $query->where('task_status', '=', 1);
        }
        return $query->count();
    }

    // 获取开发中的任务
    public function queryPending()
    {
        $query = $this->queryAll();
        $query->where('task_status','=', 1);
        return $query->getAll();
    }

    public function queryAll() {
        $query = $this->newQuery();
        $query->select('tasks.*, A.app_name, B.v_number, C.ft_name');
        $query->join("LEFT JOIN ".$this->getTableName('applications', 'A').' ON A.id = tasks.application_id');
        $query->join("LEFT JOIN ".$this->getTableName('versions', 'B').' ON B.id = tasks.version_id');
        $query->join("LEFT JOIN ".$this->getTableName('features', 'C').' ON C.id = tasks.feature_id');
        $query->order('tasks.task_status', 'ASC');
        $query->order('tasks.task_priority', 'DESC');
        $query->order('tasks.update_time', 'DESC');
        return $query;
    }

    // 获取任务关联的版本
    public function version()
    {
        // 实际开发中会查询数据库获取关联版本
        // return Version::find($this->version_id);
        return null;
    }

    // 获取任务关联的功能
    public function feature()
    {
        // 实际开发中会查询数据库获取关联功能
        // return Feature::find($this->feature_id);
        return null;
    }

    // 获取所有状态选项
    public static function getStatusOptions()
    {
        return [
            '待处理' => '待处理',
            '进行中' => '进行中',
            '已完成' => '已完成',
            '已取消' => '已取消'
        ];
    }

    // 获取任务类型选项
    public static function getTypeOptions()
    {
        return [
            '0' => '研发任务',
            '1' => 'Bug修改'
        ];
    }
}